<!DOCTYPE html>
<html>
<?php include 'page/public/header.php' ?>
<style>
    .toast-top-right{top:50px!important;right: 120px!important;}
    .topleftlogo{width: 120px!important;padding-left: 0!important;padding-top: 0!important;height: 44px !important;}
    .topleftlogo2{width: 200px!important;padding-left: 0!important;padding-top: 0!important;height: 44px !important;}
    .laytp-admin .layui-logo .title{top: 0 !important;}
    #userinfo{padding-bottom: 7px;border-bottom: 1px solid #ccc;}
    #logout{padding-top: 10px;border-top: 1px solid #ccc;}
    .rolexuan{font-size: 0!important;margin-right: 6px;position: absolute;left: 4px;color: #04c;}
    .menu-one{color: #c0c4cc !important;}
    #menu-one-top{padding-top: 10px;border-top: 1px solid #cccccc;}
    #menu-one-left a{margin-top: 10px;margin-bottom: 10px;}
    .user .layui-nav-child{padding: 10px 0;}

    .laytp-tab .layui-tab-control .layui-icon-down .layui-nav-more{right: 12px;}
    .laytp-tab .layui-tab-tool .layui-nav{}
</style>
<link rel="manifest" href="/manifest.json"/>
<script src='/static/component/laytp/module/admin.js'></script>
<script src='/static/admin/js/ws.js'></script>

<!-- 结 构 代 码 -->
<body class="layui-layout-body laytp-admin">
<!-- 布 局 框 架 -->
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <!-- 顶 部 左 侧 功 能 -->
        <ul class="layui-nav layui-layout-left" id="header-left">
            <li class="layui-nav-item collaspe"><a href="javascript:void(0);" class="layui-icon layui-icon-shrink-right"></a></li>
            <li class="layui-nav-item" id="header-left-title" style="margin-left: 10px"></li>
        </ul>
        <!-- 顶 部 右 侧 菜 单 -->
        <div id="control" menukey="1" class="layui-layout-control"></div>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item layui-hide-xs message"></li>
            <li class="layui-nav-item layui-hide" id="addSerWk" onclick="addToDesktop()" title="添加到桌面">
                <a href="javascript:;">
                    <i class="layui-icon layui-icon-add-circle-fine"></i>
                </a>
            </li>
            <li class="layui-nav-item lock">
                <a href="javascript:;">
                    <i class="layui-icon layui-icon-password" title="锁定屏幕" id="lock_ping"></i>
                </a>
            </li>
            <li class="layui-nav-item user">
                <!-- 头 像 -->
                <a href="javascript:;">
                    <img class="layui-nav-img" src="/static/admin/images/avatar.png">
                </a>
                <!-- 功 能 菜 单 -->
                <dl class="layui-nav-child">
                    <dd id="userinfoDIv"><a href="javascript:void(0);" class="userinfo" id="userinfo" style="margin-bottom: 10px">个人档案</a></dd>

                    <dd id="menu-one-top"><span class="rolexuan"><i class="layui-icon layui-icon-right"></i></span><a href="javascript:void(0);" onclick="setpagetype(this,2)">一级菜单在顶部</a></dd>
                    <dd id="menu-one-left"><span class="rolexuan"><i class="layui-icon layui-icon-right"></i></span><a href="javascript:void(0);" onclick="setpagetype(this,1)">一级菜单在左边</a></dd>
                    <dd><a href="javascript:void(0);" class="logout" id="logout">退出登录</a></dd>
                </dl>
            </li>
        </ul>
    </div>

    <!-- 侧 边 区 域 -->
    <div class="layui-side layui-bg-black" id="layui-side">
        <!-- 菜 单 顶 部 -->
        <div class="layui-logo">
            <!-- 图 标 -->
            <img class="logo" />
            <!-- 标 题 -->
            <span class="title"><img src="/static/admin/images/topleftlogo.png" class="topleftlogo"></span>
            <span class="title" style="display: none;"><img src="/static/admin/images/topleftlogo2.png" class="topleftlogo2"></span>
        </div>
        <!-- 菜 单 内 容 -->
        <div class="layui-side-scroll">
            <div id="sideMenu" menukey="1">
                <ul></ul>
            </div>
        </div>
    </div>

    <!-- 视 图 页 面 -->
    <div class="layui-body">
        <!-- 内 容 页 面 -->
        <div id="content" style="width: calc(100% - 120px) !important;min-width: 1080px;">
        </div>

        <!--版权信息-->
        <div class="footer" id="footer_box">
<!--            <span id="history">-->
<!--                <i class="layui-icon layui-icon-app"></i>-->
<!--                <span class="layui-badge" id="history_num">0</span>-->
<!--            </span>-->

            <div class="box none">
            </div>

            <a id="footer-copyright">Copyright©成都筑为科技有限公司 筑为智慧</a>
            <a id="footer-move"><i class="layui-icon layui-icon-download-circle"></i>&nbsp;移动端</a>
        </div>
    </div>

    <!-- 遮 盖 层 -->
    <div class="laytp-cover"></div>
    <!-- 加 载 动 画-->
    <div class="loader-main">
        <div class="loader"></div>
    </div>
</div>
<div id="lock_load">
    <div class="locktit"><img src="/static/admin/images/locktit.png" alt=""></div>
    <div class="lockcom">
        <input type="password" placeholder="输入密码  回车解锁" onkeydown="unsetLock(this.value)">
        <div class="gologin">切换账号登录</div>
    </div>
    <div class="lock_logo"><img src="/static/admin/images/lock_logo.png" alt=""></div>
</div>


<!-- 框 架 初 始 化 -->
<script>
    // 设置框架样式
    function setpagetype(obj,val){
        if($(obj).attr('class') != 'menu-one'){
            parent.window.location.reload();
            sessionStorage.setItem('page_style_type',val);
        }
    }

    // 判断菜单的样式
    setTimeout(function () {
        var page_style_type = sessionStorage.getItem('page_style_type');
        if(page_style_type == 2){
            $('#menu-one-top').find('a').addClass('menu-one');
            $('#menu-one-left').find('i').hide();

            $('.topleftlogo2').parent().show();
            $('.topleftlogo').parent().hide();
        }else{
            $('#menu-one-top').find('i').hide();
            $('#menu-one-left').find('a').addClass('menu-one');

            $('.topleftlogo2').parent().hide();
            $('.topleftlogo').parent().show();
        }
    },2000);


    // common.js中调用 关闭三级菜单并处理样式
    function colseSideStyle(){
        if($("#layui-side").hasClass('nav49')){
            $("#sideMenu>ul>li").each(function () {
                if($(this).find('.sideClick').attr('key') > 0){
                    $(this).find('.sideClick').attr('key',0).hide();
                }
            });
        }
    }

    // $(document).bind('click', function(e) {
    //     var e = e || window.event;
    //     var elem = e.target || e.srcElement;
    //     while (elem) {
    //         if ($(e.target).parents('#sideMenu').length != 0) {
    //             return;
    //         }
    //         elem = elem.parentNode;
    //     }
    //
    //     $('#sideMenu li').each(function () {
    //         if($(this).find('>div').length == 1){
    //             $(this).css('cssText','background-color: none !important');
    //         }
    //     });
    //
    //     $('.sideClick').hide();
    //     $('.sideClick').parent().find('a>span>i').hide();
    //     $('.sideClick').parent().removeClass('layui-this');
    // });


    // $(document).off("mouseover", "#sideMenu li").on("mouseover", "#sideMenu li", function(e) {
    //     if($('#sideMenu').attr('class') === 'laytp-nav-mini'){
    //         var text = $.trim($(this).find('a').eq(0).text());
    //         if($(this).find('div').length == 0){
    //             layer.tips(text, $(this),{time:0});
    //         }
    //     }
    //     e.stopPropagation();
    // });
    // $(document).off("mouseout", "#sideMenu li").on("mouseout", "#sideMenu li", function(e) {
    //     if($('#sideMenu').attr('class') === 'laytp-nav-mini'){
    //         layer.closeAll('tips');
    //     }
    //     e.stopPropagation();
    // });



    showLock_page();

    var watermarkTxt = "";
    layui.use(['laytp','admin','popup','context','element','jquery','notice'], function() {
        var context = layui.context;
        var element = layui.element;
        var notice = layui.notice;

        // 返回首页
        $('.layui-logo').click(function () {
            parent.openNewPage({id: 2});
        });



        // 个人档案
        $("#userinfo").click(function () {
            openPage('/page/user/userinfo.php', '80%', '80%');
        });

        // 登出逻辑
        layui.admin.logout(function(){
            facade.clearCache();
            layui.popup.success("退出成功");
            facade.redirect("/login.php");
        });
        $("#lock_load .gologin").click(function () {
            facade.redirect("/login.php");
        });


        // ajax请求，更新缓存
        facade.ajax({url: ask_getuserinfo}).done(function(res){
            if (res['code'] === 2000) {
                localStorage.clear();
                // 初始化顶部用户信息
                var user = res.data;
                if(user){
                    // connection_work(parseInt(user.id));
                    setTimeout(function () {
                        watermarkTxt = res.data.watermarktxt
                    },1000)
                    $('.user>a').append('<span id="zwusername">'+res.data.realname+'</span>');
                    if(res.data.avatar){
                        $('.user>a>img').attr('src', get_file_complete(res.data.avatar));
                    }
                    $('body').show();

                    show_roleinfo(user.rolearr, user.token_roletype)
                }
                // 设置配置信息
                layui.admin.setConfigType("yml");
                var laytpConfigYmlPath = "/static/component/laytp/config/laytp.config.yml";
                layui.admin.setConfigPath(laytpConfigYmlPath);
                layui.admin.render();
            }
        }).fail(function(){
            $('.layui-layout-admin').html('');
            $('body').show();
        });


        facade.ajax({url: ask_getuserworker}).done(function(res){
            if (res.code === 2000) {
                for (var key in res.data){
                    show_notice_worker(key, res.data[key]);
                }
                addCloseBut();
            }
        });




        function show_roleinfo(data, token_roletype) {
            var str = '';
            for (var key in data){
                if(data[key].value == token_roletype){
                    str += '<dd class="roleinfo"><span class="rolexuan"><i class="layui-icon layui-icon-radio"></i></span><a href="javascript:void(0);" class="li" key="'+data[key].value+'" style="margin-bottom: 10px;color:#0E0EFF">'+data[key].name+'</a></dd>';
                }else{
                    str += '<dd class="roleinfo"><a href="javascript:void(0);" class="li" key="'+data[key].value+'" style="margin-bottom: 10px;">'+data[key].name+'</a></dd>';
                }
            }
            $("#userinfoDIv").after(str);

            $(".roleinfo .li").click(function () {
                var value = parseInt($(this).attr('key'));
                cutuserrole(value)
            });
        }


        function cutuserrole(roletype) {
            facade.ajax({url: ask_cutuserrole, data:{roletype:roletype}}).done(function(res){
                if (res.code === 2000) {
                    facade.redirect("/index.php");
                }
            });
        }

    });




    function connection_work(uid) {
        setPublicUid(uid);
        createWebSocket();
        // ws = new WebSocket(server_socket_name);
        // ws.onopen = function() {
        //     ws.send({ type: 'pc', uid: uid});
        // };
        // ws.onmessage = function(e) {
        //     show_notice(JSON.parse(e.data))
        //     addCloseBut();
        // };
        // ws.onclose = function() {
        //     console.log("连接已关闭");
        //     setTimeout(function () {
        //         connection_work(uid);
        //     },5000)
        // }
        // ws.onerror = function () {
        //     console.log("连接错误");
        //     setTimeout(function () {
        //         connection_work(uid);
        //     },5000)
        // };
    }

    function show_notice_index(data) {
        if(!data){
            return false;
        }
        if(!data.type){
            return false;
        }

        var iconClass = 'notice-'+data.type;
        var title = data.title+'<span class="senddt">'+data.dt+'</span>';
        var mess = data.mess;

        layui.use(['laytp','notice'], function() {
            var notice = layui.notice;
            notice.success(mess, title, {
                closeButton:true,//显示关闭按钮
                debug:false,//启用debug
                positionClass:"toast-top-right",//弹出的位置,
                showDuration:"0",//显示的时间
                hideDuration:"0",//消失的时间
                timeOut:"0",//停留的时间,0则不自动关闭
                extendedTimeOut:"0",//控制时间
                showEasing:"swing",//显示时的动画缓冲方式
                hideEasing:"linear",//消失时的动画缓冲方式
                iconClass: 'layui-icon '+iconClass, // 自定义图标，有内置，如不需要则传空 支持layui内置图标/自定义iconfont类名,需要完整加上 layui-icon/icon iconfont
                onclick: function (res) {
                    var meter = data.meter || '';
                    openPage('/page/'+data.table+'/show.php?id='+data.mid+meter);
                    guanCloseBut();
                }
            });
        });
    }


    function show_notice_worker(type, data) {
        var iconClass = 'notice-'+type;
        layui.use(['laytp','notice'], function() {
            var notice = layui.notice;
            notice.success(data.mess, data.title, {
                closeButton:true,//显示关闭按钮
                debug:false,//启用debug
                positionClass:"toast-top-right",//弹出的位置,
                showDuration:"0",//显示的时间
                hideDuration:"0",//消失的时间
                timeOut:"0",//停留的时间,0则不自动关闭
                extendedTimeOut:"0",//控制时间
                showEasing:"swing",//显示时的动画缓冲方式
                hideEasing:"linear",//消失时的动画缓冲方式
                iconClass: 'layui-icon '+iconClass, // 自定义图标，有内置，如不需要则传空 支持layui内置图标/自定义iconfont类名,需要完整加上 layui-icon/icon iconfont
                onclick: function (res) {
                    openNavPage(data.title,data.href)
                    guanCloseBut()
                }
            });
        });
    }




    function addCloseBut() {
        if($("#toast-container").length > 0){
            if($("#toast-container .toast").length > 1 && $("#toast-container .notice-clear-all").length < 1){
                var str = '<div class="toast layui-icon notice-clear-all" onclick="closeAllNotice()"><div class="toast-message">全部关闭</div></div>';
                $("#toast-container").prepend(str);
            }
        }
    }

    function guanCloseBut() {
        if($("#toast-container").length > 0){
            if($("#toast-container .toast").length < 4 && $("#toast-container .notice-clear-all").length > 0){
                $("#toast-container .notice-clear-all").remove();
            }
        }
    }

    function closeAllNotice(){
        layui.use(['laytp','notice'], function() {
            layui.notice.clear();
        });
    }



    // setInterval(function() {
    //     layui.use(['laytp','admin','popup','context','element','jquery','notice'], function() {
    //         facade.ajax({url: ask_getuserinfo,showLoading:false}).done(function(res){
    //             if(res.code != 2000){
    //                 facade.redirect("/登录页面");
    //             }
    //         }).fail(function(){
    //             facade.redirect("/登录页面");
    //         });
    //     });
    // }, 300000);





    var seconds = 900,lockval;
    screenLockTask();
    function screenLockTask() {
        lockval = setInterval(function(){
            seconds -= 1;
            showLock_page();
            if(seconds < 1){
                seconds = 900;
                facade.setCookie('pmlock', 1, 365);
            }
        },1000);
    }


    function showLock_page() {
        layui.use(['laytp'], function() {
            var pmlock = facade.getCookie('pmlock') || 0;
            if(pmlock > 0){
                if ($('#lock_load').is(':hidden')) {
                    $("#lock_load").show();
                }
            }
        })
    }

    function unsetLock(value) {
        if (event.keyCode == 13) {
            layui.use(['laytp'], function() {
                facade.ajax({url: ask_getuserlock, data:{pass:value}}).done(function(res){
                    if(res.code === 2000){
                        facade.setCookie('pmlock', 0);
                        seconds = 900;
                        clearInterval(lockval);
                        screenLockTask();
                        $("#lock_load .lockcom input").val('');
                        $("#lock_load").hide();

                    }else{
                        $("#lock_load .lockcom input").val('');
                        layer.msg('解 锁 失 败', {icon: 5});return false;
                    }
                }).fail(function(){
                    $("#lock_load .lockcom input").val('');
                    layer.msg('解 锁 失 败', {icon: 5});return false;
                });
            });
        }
    }

    $("#lock_ping").click(function () {
        facade.setCookie('pmlock', 1, 365);
        showLock_page();
    });

</script>

<script>

    var deferredPrompt = null;
    window.addEventListener("beforeinstallprompt", e => {
        e.preventDefault();
        deferredPrompt = e;
    });

    window.addEventListener("appinstalled", () => {
        deferredPrompt = null;
        $("#addSerWk").addClass('layui-hide')
    });

    if ("serviceWorker" in navigator) {
        window.addEventListener("load", function () {
            navigator.serviceWorker.register('./serviceWorker.js')
                .then(function (registration) {
                    setTimeout(function () {
                        if(deferredPrompt){
                            $("#addSerWk").removeClass('layui-hide')
                        }
                    },1000)
                })
                .catch(function (err) {
                });
        })
    }


    function addToDesktop() {
        // 调用prompt()方法触发安装弹窗
        deferredPrompt.prompt();
        deferredPrompt = null;
    }









    // if (window.Notification) {
    //     var button = document.getElementById('buttonaaaaa');
    //
    //     var popNotice = function() {
    //         if (Notification.permission == "granted") {
    //             var notification = new Notification("我是标我是标题：", {
    //                 body: '我是内容：打开ID=1的项目。我是内容我是内容我是内容我是内容.',
    //                 icon: '//image.zhangxinxu.com/image/study/s/s128/mm1.jpg'
    //             });
    //
    //             notification.onclick = function() {
    //                 openPage('/page/engin/show.php?id=1','80%','80%');
    //                 notification.close();
    //             };
    //         }
    //     };
    //
    //     button.onclick = function() {
    //         if (Notification.permission == "granted") {
    //             popNotice();
    //         } else if (Notification.permission != "denied") {
    //             Notification.requestPermission(function (permission) {
    //                 popNotice();
    //             });
    //         }
    //     };
    // } else {
    //     alert('浏览器不支持Notification');
    // }
</script>
<script language="javascript" src="https://put.zoosnet.net/JS/LsJS.aspx?siteid=PUT23492095&float=1&lng=cn"></script>
</body>
</html>
